<?php

class Communitas_Integrate_Model_Customer_Address_Api extends Mage_Customer_Model_Address_Api {

	public function items($customerId) {
		$customer = Mage::getModel('customer/customer')->load($customerId);
		if (!$customer->getId()) {
			$this->_fault('customer_not_exists');
		}
		$result = array();
		foreach ($customer->getAddresses() as $address) {
			$data = $address->toArray();
			$row  = array();
			$row['address_id'] = $address->getId();
			foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
				$row[$attributeAlias] = isset($data[$attributeCode]) ? $data[$attributeCode] : null;
			}
			foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
				if (isset($data[$attributeCode])) {
					$row[$attributeCode] = $data[$attributeCode];
				}
			}
			$row['is_default_billing'] = $customer->getDefaultBillingAddress() == $address->getId();
			$row['is_default_shipping'] = $customer->getDefaultShippingAddress() == $address->getId();
			$result[] = $row;
		}
		return $result;
	}

	public function info($addressId) {
		$address = Mage::getModel('customer/address')->load($addressId);
		if (!$address->getId()) {
			$this->_fault('not_exists');
		}
		$result = array();
		$result['address_id'] = $address->getId();
		foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
			$result[$attributeAlias] = $address->getData($attributeCode);
		}
		foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
			$result[$attributeCode] = $address->getData($attributeCode);
		}
		if ($customer = $address->getCustomer()) {
			$result['is_default_billing']  = $customer->getDefaultBillingAddress() == $address->getId();
			$result['is_default_shipping'] = $customer->getDefaultShippingAddress() == $address->getId();
			$result['customer_id'] = $customer->getId();
		}
		return $result;
	}

}
